import json

class json_helper:
    '''
    Example
    -----
    >>> j = json_helper(dict_=test_dict)
    >>> j.to_json(save_dir="E:/workspace/SOMatch/preprocess/test.json")
    '''
    def __init__(self, dict_: dict=None, json_dir=None) -> None:
        if dict_ == None and json_dir == None:
            raise ValueError("both dict_ and json_dir are None!")
        if json_dir != None:
            self.json_dir = json_dir
            _ = self.from_json()
        else:
            self.dict_ = dict_


    def to_json(self, *, save_dir = "") -> None:
        with open(save_dir, 'w') as fp:
            json.dump(self.dict_, fp, indent='\t', sort_keys=True)


    def from_json(self) -> dict:
        with open(self.json_dir, 'r') as fp:
            self.dict_ = json.load(fp)

        return self.dict_


    def get_dict(self) -> dict:
        return self.dict_


    def get_content(self):
        '''
        跟get_dict相同，返回的json内容可能不止是dict
        '''
        return self.dict_